﻿// See https://aka.ms/new-console-template for more information

//using MongoDB.Entities;
//using StackExchange.Redis;
using com.epam.indigo;
using Newtonsoft.Json.Linq;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using System;
using System.Data.SqlTypes;
using System.Drawing;
using System.Reflection;
using System.Security.Cryptography;

Console.WriteLine("Hello, World!");
try
{
    // 初始化 Indigo
    var indigo = new Indigo();
    var renderer = new IndigoRenderer(indigo);
    // 设置渲染选项
    indigo.setOption("render-output-format", "png");
    indigo.setOption("render-image-size", 500, 500);
    indigo.setOption("render-margins", 20, 20);
    indigo.setOption("render-background-color", 1, 1, 1); // 白色背景
    indigo.setOption("render-coloring", true);

    // 隐藏隐式氢
    indigo.setOption("render-implicit-hydrogens-visible", false);



    // 提供的 Molfile 字符串
    string molfile = @"
     RDKit          2D

 41 41  0  0  0  0  0  0  0  0999 V2000
    9.9722   -1.2990    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    8.4722   -1.2990    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    7.7222    0.0000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    6.2222    0.0000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    6.0414   -1.4891    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
    5.5095   -2.8916    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    6.8376   -3.5887    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
    4.6574   -4.1261    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    5.7801   -5.1207    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.5346   -5.1207    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    2.2064   -5.8178    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    2.7383   -7.2204    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.7500   -6.1768    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -0.7500   -6.1768    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -2.2064   -5.8178    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -3.5346   -5.1207    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -4.6574   -4.1261    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -5.5095   -2.8916    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -6.0414   -1.4891    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -7.4978   -1.8480    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -8.5369   -0.7662    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -6.2222    0.0000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -6.0414    1.4891    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -5.5095    2.8916    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -4.6574    4.1261    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -5.7801    5.1207    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -5.4801    6.5904    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
   -6.6029    7.5851    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -3.5346    5.1207    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -2.2064    5.8178    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -0.7500    6.1768    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -0.9308    7.6659    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
    0.7500    6.1768    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.9308    7.6659    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
    2.2064    5.8178    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.5346    5.1207    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    4.3867    6.3552    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
    4.6574    4.1261    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    5.5095    2.8916    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
    6.0414    1.4891    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    7.4978    1.8480    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
  1  2  1  0
  2  3  1  0
  3  4  1  0
  4  5  1  0
  5  6  1  0
  6  7  2  0
  6  8  1  0
  8  9  1  0
  8 10  1  0
 10 11  1  0
 11 12  1  0
 11 13  1  0
 13 14  1  0
 14 15  1  0
 15 16  1  0
 16 17  1  0
 17 18  1  0
 18 19  1  0
 19 20  1  0
 20 21  1  0
 19 22  1  0
 22 23  2  3
 23 24  1  0
 24 25  2  3
 25 26  1  0
 26 27  1  0
 27 28  1  0
 25 29  1  0
 29 30  1  0
 30 31  1  0
 31 32  1  0
 31 33  1  0
 33 34  1  0
 33 35  1  0
 35 36  1  0
 36 37  1  0
 36 38  1  0
 38 39  1  0
 39 40  1  0
 40 41  2  0
 40  4  1  0
M  END
";
    // 加载 Molfile
    var molecule = indigo.loadMolecule(molfile);
    // 清理分子以优化显示（重新计算 2D 坐标）
    molecule.layout();
    var countImplicitHydrogens = molecule.countImplicitHydrogens();

    var filename = $"molecule_no_hydrogens_{DateTime.Now.Ticks}.png";
    // 渲染分子到文件
    renderer.renderToFile(molecule, filename);

    Console.WriteLine($"分子结构图像（隐式氢：{countImplicitHydrogens}）已生成并保存为 " + filename);
}
catch (Exception ex)
{
    Console.WriteLine($"错误: {ex.Message}");
}
//var eshelper = new StarESHelper.EsHelper("http://192.168.4.41:9250/", new StarESHelper.EsHelper.ElasticAuthConfig { username = "elastic", password = "9qLiTlVr5MGYMW7NVJz7" });
//eshelper.CreateIndex("demo");
////var ret = eshelper.CreateIndexDoc("demo", JObject.FromObject(new { create = new { _index = "demo", _id = Guid.NewGuid().ToString("N"), name = $"name", age = 20, desc = $"嘻嘻嘻嘻嘻嘻__{Guid.NewGuid().ToString("N")}" } }));
//List<JObject> bulkdatas = new List<JObject>();
//for (int i = 1; i < 21; i++)
//{
//    var id = Guid.NewGuid().ToString("N");
//    bulkdatas.Add(JObject.FromObject(new { create = new { _index = "demo1", _id = id, name = $"name_{i}", age = i + 20, desc = $"嘻嘻嘻嘻嘻嘻__{i}" } }));
//    //if (i % 2 == 0)
//    //{
//    //    bulkdatas.Add(JObject.FromObject(new { update = new { _index = "demo", _id = id, desc = $"嘻嘻ggg{i}ggg嘻嘻嘻嘻_ggg_{i}" } }));
//    //}
//}
//var ret = eshelper.BulkIndexDoc(bulkdatas);
//Console.WriteLine($"Success:{ret.Success}  Content:{ret.Content}");
//Console.WriteLine($"EsSQL:{ret.EsSQL}");
//Console.WriteLine("demo, EsCreateIndex ok!");
//Console.ReadKey();


//var timestampWithCheck = YYFUtils.TimestampWithCheck.GenerateTimestampWithCheck();
//Console.WriteLine("生成带校验的时间戳： " + timestampWithCheck);
//var isValid = YYFUtils.TimestampWithCheck.ValidateTimestamp(timestampWithCheck);
//Console.WriteLine("时间戳校验是否通过： " + (isValid ? "通过" : "不通过"));
//Console.ReadKey();

#region Indigo
Console.WriteLine("Hello, Indigo!");
IndigoNetImgHelper.ChemMolecule chemMolecule = new IndigoNetImgHelper.ChemMolecule(605, 375, 5);
//IndigoNetHelper.ChemMolecule chemMolecule = new IndigoNetHelper.ChemMolecule(500);
string path = "";
string errmsg = "";
string timemsg = "";
//while (true)
//{
//    Console.WriteLine("输入Smiles文件");
//    var smiles = Console.ReadLine();
//    if (string.IsNullOrWhiteSpace(smiles))
//    {
//        smiles = "O=C1C2C(=CC=CC=2)CC2C1=CC=CC=2";
//    }
//    path = AppDomain.CurrentDomain.BaseDirectory + "images\\";
//    if (!System.IO.Directory.Exists(path))
//    {
//        System.IO.Directory.CreateDirectory(path);
//    }
//    path += $"structimg_{DateTime.Now.ToString("yyyyMMddHHmmss")}.png";
//    var wk = AppDomain.CurrentDomain.BaseDirectory + "logo.png";
//    var bytes = chemMolecule.SaveToImageByte(smiles, out errmsg, out timemsg, 1, wk);
//    using (var image = SixLabors.ImageSharp.Image.Load(bytes))
//    {
//        image.Save(path, new PngEncoder());
//    }
//    Dictionary<int, int> othersize = new Dictionary<int, int>();
//    //othersize.Add(605, 375);
//    othersize.Add(242, 150);
//    othersize.Add(121, 75);
//    foreach (var item in othersize)
//    {
//        using (var image = SixLabors.ImageSharp.Image.Load(bytes))
//        {
//            // 缩放图片到指定尺寸
//            image.Mutate(ctx => ctx.Resize(new SixLabors.ImageSharp.Size(item.Key, item.Value)));
//            path = path.Replace("structimg_", $"structimg_{item.Value}_");
//            image.Save(path, new PngEncoder());
//        }
//    }
//    //path = chemMolecule.SaveToImage(smiles, out errmsg, 500);
//    Console.WriteLine("结果:" + path);
//    Console.WriteLine("错误信息:" + errmsg);
//    Console.WriteLine("耗时信息:" + timemsg);
//}

while (true)
{
    Console.WriteLine("输入mol文件");
    var mol = Console.ReadLine();
    if (string.IsNullOrWhiteSpace(mol))
    {
        #region mol
        mol = @"
     RDKit          2D

 41 41  0  0  0  0  0  0  0  0999 V2000
    9.9722   -1.2990    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    8.4722   -1.2990    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    7.7222    0.0000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    6.2222    0.0000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    6.0414   -1.4891    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
    5.5095   -2.8916    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    6.8376   -3.5887    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
    4.6574   -4.1261    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    5.7801   -5.1207    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.5346   -5.1207    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    2.2064   -5.8178    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    2.7383   -7.2204    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.7500   -6.1768    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -0.7500   -6.1768    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -2.2064   -5.8178    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -3.5346   -5.1207    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -4.6574   -4.1261    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -5.5095   -2.8916    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -6.0414   -1.4891    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -7.4978   -1.8480    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -8.5369   -0.7662    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -6.2222    0.0000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -6.0414    1.4891    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -5.5095    2.8916    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -4.6574    4.1261    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -5.7801    5.1207    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -5.4801    6.5904    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
   -6.6029    7.5851    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -3.5346    5.1207    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -2.2064    5.8178    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -0.7500    6.1768    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -0.9308    7.6659    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
    0.7500    6.1768    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.9308    7.6659    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
    2.2064    5.8178    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.5346    5.1207    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    4.3867    6.3552    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
    4.6574    4.1261    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    5.5095    2.8916    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
    6.0414    1.4891    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    7.4978    1.8480    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
  1  2  1  0
  2  3  1  0
  3  4  1  0
  4  5  1  0
  5  6  1  0
  6  7  2  0
  6  8  1  0
  8  9  1  0
  8 10  1  0
 10 11  1  0
 11 12  1  0
 11 13  1  0
 13 14  1  0
 14 15  1  0
 15 16  1  0
 16 17  1  0
 17 18  1  0
 18 19  1  0
 19 20  1  0
 20 21  1  0
 19 22  1  0
 22 23  2  3
 23 24  1  0
 24 25  2  3
 25 26  1  0
 26 27  1  0
 27 28  1  0
 25 29  1  0
 29 30  1  0
 30 31  1  0
 31 32  1  0
 31 33  1  0
 33 34  1  0
 33 35  1  0
 35 36  1  0
 36 37  1  0
 36 38  1  0
 38 39  1  0
 39 40  1  0
 40 41  2  0
 40  4  1  0
M  END
";
        #endregion
    }
    path = AppDomain.CurrentDomain.BaseDirectory + "images\\";
    if (!System.IO.Directory.Exists(path))
    {
        System.IO.Directory.CreateDirectory(path);
    }
    var wk = AppDomain.CurrentDomain.BaseDirectory + "logo150.png";

    Dictionary<string, string> optipons = new Dictionary<string, string>();
    //optipons.Add("render-implicit-hydrogens-visible", "true");//显示可见原子上的隐式氢
    var indigoHelper = new IndigoNetImgHelper.ChemMolecule(500, 500, 10, optipons: optipons);

    int[] othersize = new int[] { 150, 300 };
    indigoHelper.SaveToImage(mol, out errmsg, out timemsg, filename: "119639-92-8x500", filepath: path, watermarkpath: wk, othersize: othersize, dolayout: true);

    Console.WriteLine("结果:" + path);
    Console.WriteLine("错误信息:" + errmsg);
    Console.WriteLine("耗时信息:" + timemsg);
}
#endregion

#region YYFRedis
//Console.WriteLine("Hello, YYFRedis!");
//var redisconnstr = "192.168.3.121,password=qwe123,connectTimeout=5000,defaultDatabase=10";
//redisconnstr = "192.168.3.121,syncTimeout=500000,abortConnect=false,password=qwe123,defaultDatabase=5";
//// 连接到Redis服务器
//var redis = new YYFRedis.RedisCacheProvider(redisconnstr);
//#region 临时消费程序
//Console.WriteLine("处理哪个站点的数据？ www/m/mip");
//var site = Console.ReadLine();
//if (string.IsNullOrWhiteSpace(site)) { site = "www"; }
//Console.Title = $"{site}站消费服务 < 平台页面访问消费程序";
//// 接收消息
//while (true)
//{
//    // 阻塞方式获取消息，无消息时会等待
//    var receivedMessage = redis.QueuePop($"visitlogqueue:{site}");
//    if (receivedMessage.IsNull)
//    {
//        // 没有消息可以处理，可以休眠一会儿
//        Console.WriteLine($"{site}站暂无数据，休息");
//        System.Threading.Thread.Sleep(1000);
//    }
//    else
//    {
//        Console.WriteLine($"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}]收到消息：{receivedMessage}");
//        // 处理消息的代码...
//    }
//}
#endregion

#region 实现消息队列功能
/*
var cango = true;
do
{
    Console.WriteLine("创建什么服务？1 生产者 2 消费者");
    var input = Console.ReadLine();
    if (input == "2")
    {
        Console.Title = "消费者(收消息) < 实现消息队列功能";

        // 接收消息
        while (true)
        {
            // 阻塞方式获取消息，无消息时会等待
            var receivedMessage = redis.QueuePop("yyfredis_queue");//redis.GetDatabase().ListLeftPop("yyfredis_queue");
            if (receivedMessage.IsNull)
            {
                // 没有消息可以处理，可以休眠一会儿
                Console.WriteLine($"休息");
                System.Threading.Thread.Sleep(1000);
            }
            else
            {
                Console.WriteLine($"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}]收到消息：{receivedMessage}");
                Console.WriteLine($"具体处理数据(费时2秒) todo");
                System.Threading.Thread.Sleep(2000);
                // 处理消息的代码...
            }
        }
    }
    else
    {
        Console.Title = "生产者(发消息) < 实现消息队列功能";
        Console.WriteLine("输入发布的信息内容：");
        input = Console.ReadLine();
        var msg = "";
        if (string.IsNullOrWhiteSpace(input))
        {
            for (int i = 1; i < 500; i++)
            {
                msg = $"[{i}]加入队列：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}";
                var llp = redis.QueuePush("yyfredis_queue", msg);
                //var llp = redis.GetDatabase().ListLeftPush("yyfredis_queue", msg);
                Console.WriteLine($"当前队列数据量【{llp}】：{msg}");
                Thread.Sleep(1000);
            }
        }
        else
        {
            msg = $"[000]加入队列：{input}";
            redis.GetDatabase().ListLeftPush("yyfredis_queue", msg);
            Console.WriteLine(msg);
        }
    }
    Console.WriteLine("是否继续？");
    Console.ReadLine();
} while (cango);
*/
#endregion

#region 订阅发布
/*
Console.WriteLine("是否创建订阅服务？1是");
var input = Console.ReadLine();
if (input == "1")
{
    Console.Title = "订阅服务已开启";
    redis.Subscribe("yyfredis_channel", (channel, message) =>
    {
        Console.WriteLine($"订阅 Received message on {channel}: {message}");
    });
    // 等待订阅建立
    Console.WriteLine("等待订阅建立 Waiting for messages...");
    // 等待用户输入来退出程序
    Console.ReadLine();
}

Console.WriteLine("是否发布？0 否");
input = Console.ReadLine();
if (input != "0")
{
    Console.Title = "客户端已开启";
    var cango = true;
    do
    {
        Console.WriteLine("输入发布的信息内容：");
        input = Console.ReadLine();
        var c1 = redis.Publish("yyfredis_channel", $"发布信息：[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}]{input}");
        Console.WriteLine(c1 + "已发消息:" + input);
    } while (cango);
} 
*/
#endregion




/*
//StarMongoDbHelper
StarMongoDbHelper.DBHelper.DbConnStr = "mongodb://root:Kuujia2023$@192.168.1.47:27018";
StarMongoDbHelper.DBHelper.DbName = "kjwork";
StarMongoDbHelper.DBHelper dbhelper = new StarMongoDbHelper.DBHelper("", "");
var tmp = dbhelper.Get("users", "62b5a5ead600851fef1f0c07", new List<string>() { "username", "realname" }).Result;
Console.ReadKey();


var sif = new StarOSWatch.UseSysInfoWatch();
var status = Newtonsoft.Json.JsonConvert.SerializeObject(sif.GetServerStatus());
Console.WriteLine(status);
Console.ReadKey();
*/
Console.ReadKey();